package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import domain.User;
import domain.UserBean;
import domain.UserImg;

public class AdminDaoImpl implements AdminDao{
	
	
private SessionFactory sessionFactory;
	private LogDao  logDao;
	
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public List getUserByAccount(String account,String password){
		// TODO Auto-generated method stub
		String hql="from User where account=? and password=?";
		Session session=sessionFactory.getCurrentSession();
	
		Query q=session.createQuery(hql);
		System.out.println("account"+account);
		System.out.println("password"+password);
		q.setString(0, account);
		q.setString(1, password);
//		System.out.println(account);
		List list = q.list();
		//HibernateSessionFactory.closeSession();
		return list;
	}
	@Override
	public User getUserByAcc(String account){
		Session session=sessionFactory.getCurrentSession();
		Query q = session.createQuery("from User where ACCOUNT = ?");
		q.setString(0, account);
		List <User>users = q.list();
		User user = null;
		if(users.size()>0){
			user = users.get(0);
		}
		return user;
	}
	
	@Override
	public User getUserById(int userId){
		Session session=sessionFactory.getCurrentSession();
		User user = (User) session.get(User.class, userId);
		return user;
	}
	@Override
	public List getUserByAccount(String account){
		// TODO Auto-generated method stub
		String hql="from User where account=?";
		Session session=sessionFactory.getCurrentSession();
//		Bike bike = (Bike)session.load(Bike.class, 1);
//		System.out.println("���Խ��"+bike.getOld_new());
		Query q=session.createQuery(hql);
//		System.out.println(c);
		q.setString(0, account);
//		System.out.println(account);
		List list = q.list();
		//HibernateSessionFactory.closeSession();
		return list;
	}
	
	@Override
	public void insertAdmin(User user ,String path){
		UserImg ui = new UserImg();
		ui.setPath(path);
		user.setUi(ui);
//		User user1 = new User();
//		user.setName("test");
		Session session=sessionFactory.getCurrentSession();
		
		
//		session.saveOrUpdate(ui);
		session.save(user);
		
	}

	public LogDao getLogDao() {
		return logDao;
	}

	public void setLogDao(LogDao logDao) {
		this.logDao = logDao;
	}

	/* (non-Javadoc)
	 * @see dao.AdminDao#showIndex()
	 */
	@Override
	public List<List> showIndex() {
		// TODO Auto-generated method stub
		List<List> ll = new ArrayList();
		Session session=sessionFactory.getCurrentSession();
		List q = session.createSQLQuery("select faned_id  from fan_user group by faned_id").list();
		List<User> fanedTops = new ArrayList();
		for(int i=0; i<q.size();i++)
			fanedTops.add((User)session.get(User.class, Integer.parseInt(q.get(i).toString())));
		List items = session.createQuery("from Item order by createDate desc").setMaxResults(24).list();
		List regs = session.createQuery("from User order by createDate desc").setMaxResults(24).list();
		List loginLog = session.createQuery("from LoginLog order by loginTime desc").setMaxResults(24).list();
		ll.add(fanedTops);
		ll.add(items);
		ll.add(regs);
		ll.add(loginLog);
		return ll;
	}

	@Override
	public String updatePass(UserBean userbean,String acc) {
		// TODO Auto-generated method stub
		String mess ="成功";
	
		if(userbean.getOldPass().equals("")){
			mess = "原密码为空";
		}
		else if(userbean.getNewPass().equals("")){
			mess = "新密码为空";
			}
		else if(userbean.getNewAffirmPass().equals("")){
			mess = "确认新密码为空";
		}else{
			User user = getUserByAcc(acc);
			if(!(userbean.getOldPass().equals(user.getPassword()))){
				mess = "旧密码不正确";
			}else{
				
					if(userbean.getNewAffirmPass().equals(userbean.getNewPass())){
						Session session=sessionFactory.getCurrentSession();
						user.setPassword(userbean.getNewPass());
						session.saveOrUpdate(user);
					}else{
						mess="新密码和旧密码不一致";
				}
			}
		}
		
		return mess;
	}
}
